package com.hzeport.bgs22emduty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzeport.bgs22emduty.dto.ParamVo;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.ParamDictionaryRepository;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.entity.ParamDictionary;
import com.hzeport.bgs22emduty.outbound.db.ParamDictionaryRepositoryImpl;
import com.hzeport.bgs22emduty.service.ParamDictionaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2022-12-02 11:21:59
 */
@Service
@Slf4j
public class ParamDictionaryServiceImpl extends BaseServiceImpl<ParamDictionaryRepositoryImpl, ParamDictionary> implements ParamDictionaryService {

    @Resource
    private ParamDictionaryRepository pParamDictionaryRepository;

    @Override
    public BaseResult<List<ParamVo>> GetParamList(String paramModule)
    {
        try {
            List<ParamDictionary> pParamDictionaries = pParamDictionaryRepository.list(new QueryWrapper<ParamDictionary>().lambda()
                    .eq(ParamDictionary::getParamModule, paramModule)
                    .eq(ParamDictionary::getDelFlag, DeleteFlagEnum.NOT_DELETE.getValue()));
            List<ParamVo> paramVos = pParamDictionaries.stream().map(it -> {
                ParamVo paramVo = new ParamVo();
                BeanUtils.copyProperties(it, paramVo);
                return paramVo;
            }).collect(Collectors.toList());
            return BaseResult.ok(paramVos);
        }
        catch (Exception ex)
        {
            log.error("",ex);
            return BaseResult.failed("系统错误");
        }
    }

    @Override
    public String getNameWithModuleAndParamCode(String module, String code) {
        List<ParamDictionary> pParamDictionaries=repository.list(new LambdaQueryWrapper<ParamDictionary>()
                .eq(ParamDictionary::getParamModule,module));
        if (CollectionUtils.isEmpty(pParamDictionaries)){
            return code;
        }
        for (ParamDictionary pParamDictionary:pParamDictionaries ){
            if (pParamDictionary.getParamCode().equals(code)){
                return pParamDictionary.getParamName();
            }
        }
        return code;
    }


}
